package com.lowca.news.dao;

import java.util.Date;

import javax.annotation.Resource;

import org.junit.Test;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.exception.DataException;

import com.lowca.news.entity.User;
import com.lowca.news.entity.Role;

import static org.junit.Assert.*;

public class UserDaoImplTestCase extends BaseDaoTestCase {

	private static final Log log = LogFactory
			.getLog(UserDaoImplTestCase.class);

	@Resource
	private UserDao userDao;
	@Resource
	private RoleDao roleDao;

	@Test
	public void testInject() throws Exception {
		log.info("##################测试dao组件是否注入##################");
		assertNotNull(userDao);
	}

	@Test
	public void testSave() throws Exception {
		log.info("##################测试save方法是否成功##################");
		User user = new User();
		String username = "test_01";
		user.setUsername(username);
		user.setPassword("123123");
		user.setEmail("324242@qq.com");
		user.setRegisterTime(new Date());
		userDao.save(user);
		assertNotNull(user.getId());
		assertTrue(user.getId().longValue() > 0);
		User user2 = userDao.findUniqueByHQL(
				"from com.lowca.news.entity.User a where a.username=?",
				username);
		assertNotNull(user2);
		assertNotNull(user2.getId());
		assertEquals(user2.getId().longValue(), user.getId().longValue());
	}

	@Test
	public void testDelete() throws Exception {
		log.info("##################测试delete方法##################");
		User user = new User();
		String username = "test_01";
		user.setUsername(username);
		user.setPassword("123123");
		user.setEmail("324242@qq.com");
		user.setRegisterTime(new Date());
		userDao.save(user);
		User user2 = userDao.findUniqueByHQL(
				"from com.lowca.news.entity.User a where a.username=?",
				username);
		assertNotNull(user2);
		userDao.delete(user2);
		user2 = userDao.findUniqueByHQL(
				"from com.lowca.news.entity.User a where a.username=?",
				username);
		assertNull(user2);
	}

	@Test
	public void testUpdate() throws Exception {
		log.info("##################测试update方法##################");
		User user = new User();
		String username = "test_01";
		user.setUsername(username);
		user.setPassword("123123");
		user.setEmail("324242@qq.com");
		user.setRegisterTime(new Date());
		userDao.save(user);
		user.setUsername("test_02");
		userDao.saveOrUpdate(user);
		user = userDao.findUniqueByHQL(
				"from com.lowca.news.entity.User a where a.username=?",
				username);
		assertEquals(user.getUsername(), "test_02");
	}

	@Test(expected = DataException.class)
	public void testSaveUserInvalid() throws Exception {
		log.info("##################测试save方法保存无效对象是否抛出异常##################");
		User user = new User();
		String username = "test_01test_01test_01test_01test_01test_01test_01test_01test_01test_01test_01test_01test_01test_01test_01test_01";
		user.setUsername(username);
		user.setPassword("123123");
		user.setEmail("324242@qq.com");
		user.setRegisterTime(new Date());
		userDao.save(user);
	}
	
	

	@Test
	public void testAddRole() throws Exception {
		log.info("##################测试管理员和角色关联保存##################");
		Role role = new Role();
		role.setName("user");
		role.setDescription("描述");

		User user = new User();
		String username = "test_01";
		user.setUsername(username);
		user.setPassword("123123");
		user.setEmail("324242@qq.com");
		user.setRegisterTime(new Date());

		user.getRoles().add(role);
		role.getUsers().add(user);

		roleDao.save(role);
		userDao.save(user);
		
		assertTrue(role.getUsers() != null && role.getUsers().size() > 0);
		assertEquals(role.getUsers().iterator().next().getUsername(), username);
		assertTrue(user.getRoles() != null
				&& user.getRoles().size() == 1);
		assertEquals(user.getRoles().iterator().next().getName(), "user");
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public RoleDao getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

}
